#include "chatlistitem.h"
#include "ui_chatlistitem.h"
#include <QEvent>

ChatListItem::ChatListItem(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ChatListItem)
{
    ui->setupUi(this);
    installEventFilter(this);
}

void ChatListItem::item(Friend *data)
{
    if (data == NULL) {
        return;
    }
    this->data = data;
    ui->headLbl->setPixmap(QPixmap(data->image()));
    ui->nameLbl->setText(data->name());
    ui->timeLbl->setText(data->time());
    if (data->messages().count() > 0) {
        QFontMetrics metrics(font());
        auto new_content = metrics.elidedText(data->messages().last().content(), Qt::ElideRight, 150);
        ui->messLbl->setText(new_content);
    }
    ui->countLbl->setText(QString::number(data->count()));
    ui->widget_5->setVisible(data->count() != 0);
    ui->muteLbl->setVisible(data->mute());
}

void ChatListItem::Selected(bool selected)
{
    ui->widget->setStyleSheet(selected ? "QWidget#widget{background: #c3c3c3;}" : "QWidget#widget{background: #dfdfdf;}");
}

void ChatListItem::Checked(bool checked)
{
    this->isSelected = checked;
}

Friend *ChatListItem::item()
{
    return this->data;
}

void ChatListItem::mousePressEvent(QMouseEvent *event)
{
    Q_UNUSED(event);

    this->isSelected = true;
    ui->countLbl->setText("0");
    ui->widget_5->setVisible(false);

    emit selected(this);
}

void ChatListItem::enterEvent(QEvent *event)
{
    Q_UNUSED(event);
    Selected(true);
}

void ChatListItem::leaveEvent(QEvent *event)
{
    Q_UNUSED(event);
    if (!isSelected)
        Selected(false);
}

ChatListItem::~ChatListItem()
{
    delete ui;
}
